<template>
  <div class="admin-approval">
    <div class="header">
      <router-link to="/main/xingzheng/jiedai/process" class="back-btn">
        <i class="el-icon-arrow-left"></i>
        <span>返回</span>
      </router-link>
      <h2>行政审批</h2>
    </div>

    <div class="content">
      <el-tabs v-model="activeTab">
        <el-tab-pane label="待审批" name="pending">
          <el-table :data="pendingList" style="width: 100%">
            <el-table-column prop="applicationNo" label="申请编号" width="180"></el-table-column>
            <el-table-column prop="applicant" label="申请人" width="120"></el-table-column>
            <el-table-column prop="department" label="申请部门" width="150"></el-table-column>
            <el-table-column prop="guestName" label="接待对象" width="120"></el-table-column>
            <el-table-column prop="deptApprovalResult" label="部门审批" width="120">
              <template slot-scope="scope">
                <el-tag type="success">{{ scope.row.deptApprovalResult }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="estimatedCost" label="预计费用" width="120">
              <template slot-scope="scope">
                {{ scope.row.estimatedCost }}元
              </template>
            </el-table-column>
            <el-table-column label="操作" width="200">
              <template slot-scope="scope">
                <el-button size="mini" @click="viewDetail(scope.row)">查看</el-button>
                <el-button size="mini" type="primary" @click="handleApproval(scope.row)">审批</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <el-tab-pane label="已审批" name="completed">
          <el-table :data="completedList" style="width: 100%">
            <el-table-column prop="applicationNo" label="申请编号" width="180"></el-table-column>
            <el-table-column prop="applicant" label="申请人" width="120"></el-table-column>
            <el-table-column prop="department" label="申请部门" width="150"></el-table-column>
            <el-table-column prop="approvalResult" label="审批结果" width="120">
              <template slot-scope="scope">
                <el-tag :type="scope.row.approvalResult === '通过' ? 'success' : 'danger'">
                  {{ scope.row.approvalResult }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="approvalTime" label="审批时间" width="180"></el-table-column>
            <el-table-column label="操作" width="120">
              <template slot-scope="scope">
                <el-button size="mini" @click="viewDetail(scope.row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!-- 详情对话框 -->
    <el-dialog title="接待申请详情" :visible.sync="detailDialogVisible" width="60%">
      <div class="application-detail" v-if="currentApplication">
        <el-descriptions title="基本信息" :column="2" border>
          <el-descriptions-item label="申请编号">{{ currentApplication.applicationNo }}</el-descriptions-item>
          <el-descriptions-item label="申请人">{{ currentApplication.applicant }}</el-descriptions-item>
          <el-descriptions-item label="申请部门">{{ currentApplication.department }}</el-descriptions-item>
          <el-descriptions-item label="接待对象">{{ currentApplication.guestName }}</el-descriptions-item>
          <el-descriptions-item label="来访单位">{{ currentApplication.guestCompany }}</el-descriptions-item>
          <el-descriptions-item label="来访人数">{{ currentApplication.guestCount }}人</el-descriptions-item>
          <el-descriptions-item label="接待时间">
            {{ formatDateRange(currentApplication.receptionTime) }}
          </el-descriptions-item>
          <el-descriptions-item label="接待地点">{{ currentApplication.location }}</el-descriptions-item>
          <el-descriptions-item label="接待类型">{{ currentApplication.receptionType }}</el-descriptions-item>
          <el-descriptions-item label="预计费用">{{ currentApplication.estimatedCost }}元</el-descriptions-item>
        </el-descriptions>

        <div class="detail-section">
          <h4>接待事由</h4>
          <p>{{ currentApplication.purpose }}</p>
        </div>

        <div class="detail-section">
          <h4>费用说明</h4>
          <p>{{ currentApplication.costDescription }}</p>
        </div>

        <div class="detail-section" v-if="currentApplication.specialRequirements">
          <h4>特殊要求</h4>
          <p>{{ currentApplication.specialRequirements }}</p>
        </div>

        <div class="detail-section" v-if="currentApplication.approvalHistory">
          <h4>审批记录</h4>
          <el-timeline>
            <el-timeline-item
              v-for="(history, index) in currentApplication.approvalHistory"
              :key="index"
              :type="history.result === '通过' ? 'success' : 'danger'"
              :timestamp="history.time">
              {{ history.approver }} {{ history.action }}
              <p v-if="history.comment" class="approval-comment">{{ history.comment }}</p>
            </el-timeline-item>
          </el-timeline>
        </div>
      </div>
    </el-dialog>

    <!-- 审批对话框 -->
    <el-dialog title="行政审批" :visible.sync="approvalDialogVisible" width="40%">
      <el-form :model="approvalForm" label-width="100px">
        <el-form-item label="审批结果">
          <el-radio-group v-model="approvalForm.result">
            <el-radio label="通过">通过</el-radio>
            <el-radio label="拒绝">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="预算调整">
          <el-input-number v-model="approvalForm.adjustedCost" :min="0" :step="100"></el-input-number>
          <span class="unit">元</span>
        </el-form-item>
        <el-form-item label="审批意见">
          <el-input type="textarea" v-model="approvalForm.comment" rows="4"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="approvalDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitApproval">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 'pending',
      pendingList: [
        {
          applicationNo: 'JD202403001',
          applicant: '张三',
          department: '技术部',
          guestName: '李总',
          guestCompany: 'XX科技公司',
          guestCount: 3,
          receptionTime: ['2024-03-20 14:00:00', '2024-03-20 17:00:00'],
          purpose: '项目合作洽谈',
          location: '公司会议室',
          receptionType: '商务接待',
          estimatedCost: 2000,
          costDescription: '包含餐饮、茶点等费用',
          specialRequirements: '需要准备投影仪',
          deptApprovalResult: '通过'
        }
      ],
      completedList: [
        {
          applicationNo: 'JD202403002',
          applicant: '李四',
          department: '销售部',
          approvalResult: '通过',
          approvalTime: '2024-03-15 10:30:00',
          approvalHistory: [
            {
              time: '2024-03-15 09:30:00',
              approver: '王经理',
              action: '部门审批通过',
              result: '通过',
              comment: '同意接待安排'
            },
            {
              time: '2024-03-15 10:30:00',
              approver: '张行政',
              action: '行政审批通过',
              result: '通过',
              comment: '预算合理，审批通过'
            }
          ]
        }
      ],
      detailDialogVisible: false,
      approvalDialogVisible: false,
      currentApplication: null,
      approvalForm: {
        result: '通过',
        adjustedCost: 0,
        comment: ''
      }
    }
  },
  methods: {
    formatDateRange(range) {
      if (!range || !range.length) return ''
      return `${range[0]} 至 ${range[1]}`
    },
    viewDetail(row) {
      this.currentApplication = row
      this.detailDialogVisible = true
    },
    handleApproval(row) {
      this.currentApplication = row
      this.approvalForm = {
        result: '通过',
        adjustedCost: row.estimatedCost,
        comment: ''
      }
      this.approvalDialogVisible = true
    },
    submitApproval() {
      if (!this.approvalForm.comment) {
        this.$message.error('请填写审批意见')
        return
      }

      // TODO: 提交审批结果到后端
      const approvalHistory = {
        time: new Date().toLocaleString(),
        approver: '当前用户',
        action: this.approvalForm.result === '通过' ? '行政审批通过' : '行政审批拒绝',
        result: this.approvalForm.result,
        comment: this.approvalForm.comment
      }

      this.currentApplication.approvalResult = this.approvalForm.result
      this.currentApplication.approvalTime = approvalHistory.time
      this.currentApplication.approvalHistory = this.currentApplication.approvalHistory || []
      this.currentApplication.approvalHistory.push(approvalHistory)
      this.currentApplication.estimatedCost = this.approvalForm.adjustedCost

      // 从待审批列表移动到已审批列表
      const index = this.pendingList.findIndex(item => item.applicationNo === this.currentApplication.applicationNo)
      if (index > -1) {
        this.pendingList.splice(index, 1)
        this.completedList.unshift(this.currentApplication)
      }

      this.$message.success('审批提交成功')
      this.approvalDialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.admin-approval {
  padding: 20px;

  .header {
    margin-bottom: 30px;
    position: relative;
    text-align: center;

    .back-btn {
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      text-decoration: none;
      color: #606266;
      display: flex;
      align-items: center;
    }

    h2 {
      margin: 0;
      font-size: 24px;
      color: #303133;
    }
  }

  .content {
    background: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  }

  .detail-section {
    margin-top: 20px;

    h4 {
      margin: 0 0 10px;
      color: #303133;
    }

    p {
      margin: 0;
      color: #606266;
      line-height: 1.6;
    }

    .approval-comment {
      margin: 5px 0 0;
      font-size: 13px;
    }
  }

  .unit {
    margin-left: 10px;
    color: #606266;
  }
}
</style> 